package ch.uzh.ifi.ddis.ds2011.a3;

/*  
 *  @author Philip Stutz
 * 
 *  Copyright 2011 University of Zurich
 *      
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *         http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
import java.util.HashSet;
import java.util.Set;

import com.signalcollect.javaapi.DataGraphVertex;
import java.util.Iterator;

/**
 * The class AccumulatingVertex describes a vertex that receives sets of items
 * of type G in a Signal/Collect computation and just adds all those items to
 * its own state, which is a Set<G> too.
 * 
 * DataGraphVertex<VertexIdType=Object, VertexStateType=Set<G>,
 * TypeOfReceivedSignals=Set<G>
 * 
 * @param <G>
 *            � � � � � �the generic type of items received/accumulated by this
 *            vertex
 */
@SuppressWarnings("serial")
public class AccumulatingVertex<G> extends
		DataGraphVertex<Object, Set<G>, Set<G>> {

	/**
	 * Instantiates a new accumulating vertex with an empty set as initial
	 * state.
	 * 
	 * @param vertexId
	 *            the unique identifier of the vertex
	 */
	public AccumulatingVertex(Object vertexId) {
		super(vertexId, new HashSet<G>());
	}

	/**
	 * Instantiates a new accumulating vertex.
	 * 
	 * @param vertexId
	 *            the unique identifier of the vertex
	 * @param initialState
	 *            the initial state of this vertex, an instance of Set<G>
	 */
	public AccumulatingVertex(Object vertexId, Set<G> initialState) {
		super(vertexId, initialState);
	}

	/**
	 * Calculates the new state of this vertex by adding all the received
	 * elements in signals to its vertex state.
	 * 
	 * @param oldState
	 *            the previous vertex state
	 * @param uncollectedSets
	 *            the uncollected sets of items received by this vertex
	 * @return the new vertex state containing all the received set elements
	 */
    @Override
	public Set<G> collect(Set<G> oldState, Iterable<Set<G>> uncollectedSets) {
		//new Set contains all the old States
            Set<G> newState = new HashSet<G>();
            newState.addAll(oldState);
            
            // new Set collects the uncollected States to the old States
            Iterator multipleSetsIt = uncollectedSets.iterator();
            Set<G> uncolSet = (Set<G>)multipleSetsIt.next();
             
            // iterates through the set of uncollected sets and add all the the new set
            while (uncolSet != null) { 
                newState.addAll(uncolSet);
                uncolSet = (Set<G>)multipleSetsIt.next();
            } 
            return newState;
	}
}
